import { E2LProtocol, L2EProtocol } from "../proto/ELProtocol";
import { gameVersion, noticeUrl } from "../utils/CommonUtils";
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import {
    NotificationCenter
} from "../utils/NotificationCenter";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    msgBox: cc.Node = null;

    @property(cc.Label)
    versionLabel: cc.Label = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        NotificationCenter.Instance.on(L2EProtocol.l2e_login, this.completeLogin, this);
        NotificationCenter.Instance.on(L2EProtocol.l2e_login_error, this.loginError, this);
    }

    onDestroy() {
        NotificationCenter.Instance.off(L2EProtocol.l2e_login, this.completeLogin, this);
        NotificationCenter.Instance.off(L2EProtocol.l2e_login_error, this.loginError, this);
    }

    start () {
        let notice: any = null;
        let self = this;
        cc.assetManager.loadRemote(noticeUrl, (err: Error, jsonAsset: JSON) =>{
            if (err) {
                console.log(err);
                return;
            }
            notice = jsonAsset;

            if (notice != null && notice.status == 1) {
                self.showMsg(notice.title, notice.content);
            } 
        });
        this.versionLabel.string = 'ver:'+gameVersion;

        NotificationCenter.Instance.emit(E2LProtocol.e2l_read_configs);
    }

    // update (dt) {}

    public clickLogin() {
        // // 普通登录
        // NotificationCenter.Instance.emit("requestLogin");
        // cc.log("请求微信登陆");
        // jsb.reflection.callStaticMethod("com/bbl/pineapple/JniHelper", "wxLogin", "()V");
    }

    private loginError(msg: string) {
        let label_state = this.node.getChildByName('label_state');
        label_state.active = true;
        label_state.getComponent(cc.Label).string = msg;
    }

    private completeLogin(data: any) {
        // let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        // let user = logicCenter.getUserInfo();

        if (cc.sys.WECHAT_GAME == cc.sys.platform) {
            // let loadNode = this.node.getChildByName('load_bar');
            // loadNode.active = true;
            // let loadBar = loadNode.getComponent(cc.ProgressBar);
    
            // let label_state = this.node.getChildByName('label_state');
            // label_state.active = true;
            // label_state.getComponent(cc.Label).string = '加载中';
            // // if (data.tgetCoin > 0) {
            //     window['wx'].setUserCloudStorage({
            //         KVDataList: [{key: 'tgetCoin', value: String(user.tgetCoin)}],
            //         success: (res) => {
            //             console.log(res);
            //         },
            //         fail: (res) => {
            //             console.log(res);
            //         }
            //     });
            // // }
            // window['wx'].setUserCloudStorage({
            //     KVDataList: [{key: 'tgetMoney', value: String(user.tgetCoin)}],
            //     success: (res) => {
            //         console.log(res);
            //     },
            //     fail: (res) => {
            //         console.log(res);
            //     }
            // });
            // let subPackNames = ['active', 'audio', 'battle', 'font', 'hall','Particle','resources','Texture'];
            // let subCount = 0;
            // for (let i = 0; i < subPackNames.length; i++) {
            //     const packName = subPackNames[i];
            //     cc.loader.downloader.loadSubpackage(
            //         packName,
            //         progressInfo => {
            //             // 在这里处理加载进度
            //             console.log(progressInfo.progress);
                        
            //         },
            //         (error: Error) => {
            //             if (error) {
            //                 cc.error(error);
            //                 return;
            //             }
            //             subCount++;
            //             loadBar.progress = subCount/subPackNames.length;
            //             if (subCount == subPackNames.length) {
            //                cc.director.loadScene("hall");
            //             }
            //         }
            //     );
            // }
        }else{
            cc.director.loadScene("Hall");
        }
    }

    public showMsg(title: string, content: string) {
        let label_title = cc.find('notice_bg/label_title', this.msgBox);
        if (label_title != null && label_title != undefined) {
            label_title.getComponent(cc.Label).string = title;
        }
        let label_content = cc.find('notice_bg/label_content', this.msgBox);
        if (label_content != null && label_content != undefined) {
            label_content.getComponent(cc.Label).string = content;
        }
        this.msgBox.active = true;
    }

    public closeMsg() {
        this.msgBox.active = false;
    }
}

